var protocalTypes = {
		GetCompany:1,
		GetProjects:2,
		GetEmployees:3,
		GetDeliveries:4,
		GetDishes:5,
		AddProject:6,
		
		PushCompany:1001,
		PushProjects:1002,
		PushEmployees:1003,
		PushDeliveries:1004,
		PushDishes:1005,
		PushProject:1006
};

//------------------------------------------Get Company--------------------------------
function GetCompanyMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.GetCompany;
}
extendz(GetCompanyMessage,BaseProtocal)

function PushCompanyMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.PushCompany;
	this.company;
}
extendz(PushCompanyMessage,BaseProtocal);
PushCompanyMessage.prototype.read = function(data) {
	this.company = data;
}

function PushCompanyHandler() {
	BaseHandler.call(this);
}
extendz(PushCompanyHandler,BaseHandler);
PushCompanyHandler.prototype.process = function(protocal) {
	$('#company_title').html(protocal.company.name);
	$('#expand_but').hide();
	onCompanyReady(protocal.company.id);
}

//------------------------------------------Get Projects--------------------------------
function GetProjectsMessage(companyId) {
	BaseProtocal.call(this);
	this.type = protocalTypes.GetProjects;
	this.companyId = companyId;
}
extendz(GetProjectsMessage,BaseProtocal);
GetProjectsMessage.prototype.write = function() {
	return this.companyId;
}

function PushProjectsMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.PushProjects;
	this.projects;
}
extendz(PushProjectsMessage,BaseProtocal);
PushProjectsMessage.prototype.read = function(data) {
	this.projects = data;
}

function PushProjectsHandler() {
	BaseHandler.call(this);
}
extendz(PushProjectsHandler,BaseHandler);
PushProjectsHandler.prototype.process = function(protocal) {
	var projects = protocal.projects;
	var projectsHTML = "";
	for(var i in projects) {
		var project = projects[i];
		projectsHTML = projectsHTML + "<li onclick='onProjectSelected("+ project.id +")'>" + project.name + "</li>";
	}
	$('#projects').html(projectsHTML);
}

//--------------------------Get Employees--------------------------------
function GetEmployeesMessage(projectId) {
	BaseProtocal.call(this);
	this.type = protocalTypes.GetEmployees;
	this.projectId = projectId;
}
extendz(GetEmployeesMessage,BaseProtocal);
GetEmployeesMessage.prototype.write = function() {
	return this.projectId;
}

function PushEmployeesMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.PushEmployees;
	this.employees;
}
extendz(PushEmployeesMessage,BaseProtocal);
PushEmployeesMessage.prototype.read = function(data) {
	this.employees = data;
}

function PushEmployeesHandler() {
	BaseHandler.call(this);
}
extendz(PushEmployeesHandler,BaseHandler);
PushEmployeesHandler.prototype.process = function(protocal) {
	var employees = protocal.employees;
	var employeesHTML = "";
	for(var i in employees) {
		var employee = employees[i];
		employeesHTML = employeesHTML + "<tr><td>"+ employee.name +"</td></tr>";
	}
	$("#employeesHeader").show();
	$('#employees').html(employeesHTML);
}

//-------------------------------Get Deliveries------------------------------------
function GetDeliveriesMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.GetDeliveries;
}
extendz(GetDeliveriesMessage,BaseProtocal);

function PushDeliveriesMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.PushDeliveries;
	this.deliveries;
}
extendz(PushDeliveriesMessage,BaseProtocal);
PushDeliveriesMessage.prototype.read = function(data) {
	this.deliveries = data;
}

function PushDeliveriesHandler() {
	BaseHandler.call(this);
}
extendz(PushDeliveriesHandler,BaseHandler);
PushDeliveriesHandler.prototype.process = function(protocal) {
	var deliveries = protocal.deliveries;
	var deliveriesHTML = "";
	for(var i in deliveries) {
		var delivery = deliveries[i];
		deliveriesHTML = deliveriesHTML + "<li onclick='onDeliverySelected("+ delivery.id +")'>" + delivery.name + "</li>";
	}
	$('#deliveries').html(deliveriesHTML);
}

//-------------------------------Get Dishes------------------------------------
function GetDishesMessage(deliveryId) {
	BaseProtocal.call(this);
	this.type = protocalTypes.GetDishes;
	this.deliveryId = deliveryId;
}
extendz(GetDishesMessage,BaseProtocal);
GetDishesMessage.prototype.write = function() {
	return this.deliveryId;
}

function PushDishesMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.PushDishes;
	this.dishes;
}
extendz(PushDishesMessage,BaseProtocal);
PushDishesMessage.prototype.read = function(data) {
	this.dishes = data;
}

function PushDishesHandler() {
	BaseHandler.call(this);
}
extendz(PushDishesHandler,BaseHandler);
PushDishesHandler.prototype.process = function(protocal) {
	var dishes = protocal.dishes;
	var dishesHTML = "";
	for(var i in dishes) {
		var dish = dishes[i];
		dishesHTML = dishesHTML + "<tr><td>"+ dish.name +"</td><td>" + dish.price + "</td></tr>";
	}
	$("#dishesHeader").show();
	$('#dishes').html(dishesHTML);
}

//-------------------Add Project----------------------------------------
function AddProjectMessage(projectName,projectIntro) {
	BaseProtocal.call(this);
	this.type = protocalTypes.AddProject;
	this.projectName = projectName;
	this.projectIntro = projectIntro;
}
extendz(AddProjectMessage,BaseProtocal);
AddProjectMessage.prototype.checkBeforeSend = function() {
	return !isnull(this.projectName);
}
AddProjectMessage.prototype.write = function() {
	return {projectName : this.projectName,projectIntro: this.projectIntro};
}

function PushProjectMessage() {
	BaseProtocal.call(this);
	this.type = protocalTypes.PushProject;
	this.project;
}
extendz(PushProjectMessage,BaseProtocal);
PushProjectMessage.prototype.read = function(data) {
	this.project = data;
}

function PushProjectHandler() {
	BaseHandler.call(this);
}
extendz(PushProjectHandler,BaseHandler);
PushProjectHandler.prototype.process = function(protocal) {
	var project = protocal.project;
	var html = "<li onclick='onProjectSelected("+ project.id +")'>" + project.name + "</li>";
	$("#projects").html($("#projects").html() + html);
}

function initHandler(handler) {
	handler.register(protocalTypes.PushCompany,PushCompanyMessage,new PushCompanyHandler());
	handler.register(protocalTypes.PushProjects,PushProjectsMessage,new PushProjectsHandler());
	handler.register(protocalTypes.PushEmployees,PushEmployeesMessage,new PushEmployeesHandler());
	handler.register(protocalTypes.PushDeliveries,PushDeliveriesMessage,new PushDeliveriesHandler());
	handler.register(protocalTypes.PushDishes,PushDishesMessage,new PushDishesHandler());
	handler.register(protocalTypes.PushProject,PushProjectMessage,new PushProjectHandler);
}